<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>通路管理</title>
<meta name="decorator" content="default" />
<%@include file="/WEB-INF/views/include/treeview.jsp"%>
<script type="text/javascript">
	var setting = {
		check: {
			enable: true,
			chkboxType:  { "Y" : "ps", "N" : "" },
		}
	};
	var zNodes =[<c:forEach items="${offices}" var="office">
	{id:"${office.id}", pId:"${not empty office.parent.id?office.parent.id:0}", name:"${not empty office.parent.id?office.name:'二级商家'}"}, </c:forEach>];
	var ids=[<c:forEach items="${route.officeIdsSet}" var="id">"${id}", </c:forEach>];
	$(document).ready(function() {
		var tree=$.fn.zTree.init($("#officeTree"), setting, zNodes);
		// 默认选择节点
		for(var i=0; i<ids.length; i++) {
			var node = tree.getNodeByParam("id", ids[i]);
			try{tree.checkNode(node, true, false);}catch(e){}
		}
		
		$("#value").focus();
		$("#routeForm").validate({
			submitHandler: function(form){
				var ids = [], nodes = tree.getCheckedNodes(true);
				for(var i=0; i<nodes.length; i++) {
					ids.push(nodes[i].id);
				}
				$("#officeIdsSet").val(ids);
				loading('正在提交，请稍等...');
				form.submit();
			},
			errorContainer: "#messageBox",
			errorPlacement: function(error, element) {
				$("#messageBox").text("输入有误，请先更正。");
				if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
			}
		});
	});
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/sys/route/">通路列表</a></li>
		<li class="active"><a href="${ctx}/sys/route/form?id=${route.id}">通路<shiro:hasPermission
					name="sys:route:edit">${not empty route.id?'修改':'添加'}</shiro:hasPermission>
				<shiro:lacksPermission name="sys:route:edit">查看</shiro:lacksPermission></a></li>
	</ul>
	<br />
	<form:form id="routeForm" modelAttribute="route"
		action="${ctx}/sys/route/save" method="post" class="form-horizontal">
		<form:hidden path="id" />
		<tags:message content="${message}" />
		<div class="control-group">
			<label class="control-label">名称:</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="50"
					class="required" />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">排序:</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" maxlength="11"
					class="required digits" />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">描述:</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" maxlength="50" rows="3" 
					class="required input-xlarge"  />
			</div>
		</div>
		<c:if test="${not empty route.id}">
			<div class="control-group">
				<label class="control-label">选择机构:</label>
				<div class="controls">
					<div><font color='red'>您选择二级商家机构后，相应的下级机构都会归属到该通路</font></div>
					<div id="officeTree" class="ztree" style="margin-top: 3px; float: left;"></div>
					<form:hidden path="officeIdsSet" />
				</div>
			</div>
		</c:if>
		<div class="form-actions">
			<shiro:hasPermission name="sys:route:edit">
				<input id="btnSubmit" class="btn btn-primary" type="submit"
					value="保 存" />&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回"
				onclick="history.go(-1)" />
		</div>
	</form:form>
</body>
</html>